package com.clannad.eduservice.controller;


import com.clannad.commonutils.R;
import com.clannad.eduservice.entity.EduChapter;
import com.clannad.eduservice.entity.chapter.ChapterVo;
import com.clannad.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author clannad
 * @since 2022-08-11
 */
@RestController
@RequestMapping("/eduservice/chapter")
@Api(tags = "课程大纲")
//@CrossOrigin
public class EduChapterController {
    @Autowired
    EduChapterService chapterService;

    @GetMapping("getChapterVideo/{courseId}")
    @ApiOperation("查询课程大纲")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list=chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
    }

    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter byId = chapterService.getById(chapterId);
        return R.ok().data("chapter",byId);
    }

    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean flag=chapterService.deleteChapter(chapterId);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }

    }
}

